﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using SZHL.UI.AppCode;
using System.Data;

namespace SZHL.UI.API
{
    public class TXSXManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(TXSXManage).GetMethod(msg.Action.ToUpper());
            TXSXManage model = new TXSXManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }

        /// <summary>
        /// 添加提醒事项
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void ADDTXSX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            ZH_TXSX txsx = JsonConvert.DeserializeObject<ZH_TXSX>(P1);

            if (string.IsNullOrEmpty(txsx.Type))
            {
                msg.ErrorMsg = "提醒方式不能为空";
                return;
            }
            if (string.IsNullOrEmpty(txsx.TXUser))
            {
                msg.ErrorMsg = "提醒人不能为空";
                return;
            }
            if (string.IsNullOrEmpty(txsx.TXContent))
            {
                msg.ErrorMsg = "提醒内容不能为空";
                return;
            }
            if (string.IsNullOrEmpty(txsx.TXType))
            {
                msg.ErrorMsg = "提醒模式不能为空";
                return;
            }
            else
            {
                if (txsx.TXType != "0")
                {
                    if (string.IsNullOrEmpty(txsx.Hour) || string.IsNullOrEmpty(txsx.Minute))
                    {
                        msg.ErrorMsg = "提醒时间不能为空";
                        return;
                    }
                    if (txsx.TXType == "1")
                    {
                        if (string.IsNullOrEmpty(txsx.Date))
                        {
                            msg.ErrorMsg = "仅一次模式时，提醒日期不能为空";
                            return;
                        }
                    }
                    if (txsx.TXType == "4")
                    {
                        if (string.IsNullOrEmpty(txsx.Days))
                        {
                            msg.ErrorMsg = "自定义模式时，提醒日期不能为空";
                            return;
                        }
                    }
                    if (txsx.TXType != "1")
                    {
                        if (string.IsNullOrEmpty(txsx.CFType))
                        {
                            msg.ErrorMsg = "重复模式时，重复方式不能为空";
                            return;
                        }
                        else
                        {
                            if (txsx.CFType == "2")
                            {
                                if (txsx.CFCount==null||txsx.CFCount==0)
                                {
                                    msg.ErrorMsg = "重复次数不能为空或为0";
                                    return;
                                }
                            }
                            else if (txsx.CFType == "3")
                            {
                                if (txsx.CFJZDate == null)
                                {
                                    msg.ErrorMsg = "截止时间不能为空";
                                    return;
                                }
                            }
                        }
                    }
                }
            }


            if (txsx.ID == 0)
            {
                string strID = context.Request["ID"] ?? "";
                string strCode = context.Request["Code"] ?? "";

                if (!string.IsNullOrEmpty(strID) && !string.IsNullOrEmpty(strCode))
                {
                    txsx.WXLink= WXHelp.GetWxLink(strCode, strID);
                }
                txsx.Status = "0";
                txsx.CRDate = DateTime.Now;
                txsx.CRUser = UserInfo.User.UserName;
                txsx.CRUserRealName = UserInfo.User.UserRealName;
                new ZH_TXSXB().Insert(txsx);
            }
            else
            {
                txsx.UpdateDate = DateTime.Now;
                txsx.UpdateUser = UserInfo.User.UserName;
                txsx.UpdateRealName = UserInfo.User.UserRealName;
                new ZH_TXSXB().Update(txsx);
            }
            msg.Result = txsx;
        }

        /// <summary>
        /// 获取提醒事项列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTXSXDATA(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = "  1=1 ";
            if (P1 != "")
            {
                strWhere += string.Format(" and Status='{0}'", P1);
            }
            string branch = context.Request["b"] ?? "";
            if (branch != "")
            {

                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser == "")
                {
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branch);
                }
                strWhere += string.Format(" And  CRUser in('{0}')", strQueryUser.ToFormatLike());
            }
            else
            {
                strWhere += string.Format(" And CRUser='{0}' ", UserInfo.User.UserName);
            }
            

            string strContent = context.Request["Content"] ?? "";
            if (strContent != "")
            {
                strWhere += string.Format(" and ( TXContent like '%{0}%' or TXUser  like '%{0}%' )", strContent);
            }

            

            DataTable dtList = new DataTable();
            //dtList = new ZH_XMGLB().GetDTByCommand(" select *  FROM ZH_XMGL " + strWhere + " ORDER BY LeiBie DESC,CRDate DESC ");
            int page = 0;
            int.TryParse(context.Request.QueryString["p"] ?? "1", out page);
            page = page == 0 ? 1 : page;
            int total = 0;
            dtList = new ZH_XTGGB().GetDataPager("ZH_TXSX", "* ", 8, page, "CRDate desc", strWhere, ref total);



            msg.Result = dtList;
            msg.Result1 = total;
        }

        /// <summary>
        /// 查看提醒的详细
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTXSXMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strFormID = P1;
            ZH_TXSX Model = new ZH_TXSXB().GetEntity(d => d.ID.ToString() == strFormID);
            msg.Result = Model;
        }

        /// <summary>
        /// 删除提醒事项
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELTXSX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            new ZH_TXSXB().Delete(D => D.ID.ToString() == P1);
        }

        /// <summary>
        /// 查看当月的提醒
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTXSXDATA_BY(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strWhere = " TXType='1'";
           

            string strQueryUser = context.Request["QUSER"] ?? "";
            if (strQueryUser != "")
            {
                if (strQueryUser.IndexOf(',') != -1)
                {
                    strQueryUser = "0";
                }
                strWhere += string.Format(" and CRUser in('{0}') ", strQueryUser.ToFormatLike());
            }
            else
            {
                strWhere += string.Format(" and CRUser='{0}'", UserInfo.User.UserName);
            }
            //strWhere += string.Format(" CRUser='{0}' ", UserInfo.User.UserName);

            if (P1 != "")
            {
                strWhere += string.Format(" and Status='{0}' ", P1);
            }

            string start = context.Request["start"];
            string end = context.Request["end"];
            if (!string.IsNullOrEmpty(start))
            {
                strWhere += string.Format(" and [Date]>='{0}' ", start);
            }
            if (!string.IsNullOrEmpty(end))
            {
                strWhere += string.Format(" and [Date]<='{0}' ", end);
            }




            DataTable dtList = new ZH_TXSXB().GetDTByCommand(" select ID,left([TXContent],10)+' '+[Hour]+':'+[Minute] as title,[Date]+' '+[Hour]+':'+[Minute]+':00' as start  FROM ZH_TXSX where " + strWhere + " ORDER BY CRDate ");

            msg.Result = dtList;

        }
        /// <summary>
        /// 首页获取日程提醒
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTXSX_INDEX(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format("select top 5 * from ZH_TXSX where ','+TXUser+','  like '%," + UserInfo.User.UserName + ",%' and status=0");
            msg.Result = new ZH_TXSXB().GetDTByCommand(strSql);
        }
    }
}